import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useFriendRequestStore = defineStore('friend-request', () => {
  const friendRequest = ref([]);

  const ifUpdate = ref(false);
  const update = ()=>{
    ifUpdate.value = true;
  }
  const Noupdate = ()=>{
    ifUpdate.value = false;
  }

  const initRequests =(requests)=>{
    friendRequest.value = requests;
  }

  const updateRequests = (newRequest)=>{
      const requ = {
        id: Date.now().toString(),
        userId: newRequest.senderId,
        friendId: newRequest.receiverId,
        status: 0,
        avatar: 'https://picsum.photos/200/200',
      }
      friendRequest.value = [...friendRequest.value,requ];
  }

  const acceptStatus = (userId)=>{
    const index = friendRequest.value.findIndex(f => f.userId === userId);
      if (index !== -1) {
        friendRequest.value[index].status = 1; // 直接修改原数组中的元素
      }
  }

  const refuseStatus = (userId)=>{
    const index = friendRequest.value.findIndex(f => f.userId === userId);
      if (index !== -1) {
        friendRequest.value[index].status = 2; // 直接修改原数组中的元素
      }
  }


  return {
    friendRequest,
    ifUpdate,
    update,
    Noupdate,
    updateRequests,
    acceptStatus,
    refuseStatus,
    initRequests
  };
});
